package org.dromara.netbar.service.impl;

import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.mybatis.core.page.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.dromara.netbar.domain.bo.NetbarProductStockLogBo;
import org.dromara.netbar.domain.vo.NetbarProductStockLogVo;
import org.dromara.netbar.domain.NetbarProductStockLog;
import org.dromara.netbar.mapper.NetbarProductStockLogMapper;
import org.dromara.netbar.service.INetbarProductStockLogService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 网吧商品库存流水Service业务层处理
 *
 * @author Lion Li
 * @date 2025-08-21
 */
@Slf4j
@RequiredArgsConstructor
@Service
public class NetbarProductStockLogServiceImpl implements INetbarProductStockLogService {

    private final NetbarProductStockLogMapper baseMapper;

    /**
     * 查询网吧商品库存流水
     *
     * @param logId 主键
     * @return 网吧商品库存流水
     */
    @Override
    public NetbarProductStockLogVo queryById(Long logId){
        return baseMapper.selectVoById(logId);
    }

    /**
     * 分页查询网吧商品库存流水列表
     *
     * @param bo        查询条件
     * @param pageQuery 分页参数
     * @return 网吧商品库存流水分页列表
     */
    @Override
    public TableDataInfo<NetbarProductStockLogVo> queryPageList(NetbarProductStockLogBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<NetbarProductStockLog> lqw = buildQueryWrapper(bo);
        Page<NetbarProductStockLogVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询符合条件的网吧商品库存流水列表
     *
     * @param bo 查询条件
     * @return 网吧商品库存流水列表
     */
    @Override
    public List<NetbarProductStockLogVo> queryList(NetbarProductStockLogBo bo) {
        LambdaQueryWrapper<NetbarProductStockLog> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<NetbarProductStockLog> buildQueryWrapper(NetbarProductStockLogBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<NetbarProductStockLog> lqw = Wrappers.lambdaQuery();
        lqw.orderByAsc(NetbarProductStockLog::getLogId);
        lqw.eq(bo.getProductId() != null, NetbarProductStockLog::getProductId, bo.getProductId());
        lqw.eq(bo.getWarehouseId() != null, NetbarProductStockLog::getWarehouseId, bo.getWarehouseId());
        lqw.eq(StringUtils.isNotBlank(bo.getChangeType()), NetbarProductStockLog::getChangeType, bo.getChangeType());
        lqw.eq(bo.getChangeCount() != null, NetbarProductStockLog::getChangeCount, bo.getChangeCount());
        lqw.eq(bo.getCurrentStock() != null, NetbarProductStockLog::getCurrentStock, bo.getCurrentStock());
        lqw.eq(StringUtils.isNotBlank(bo.getRelatedOrder()), NetbarProductStockLog::getRelatedOrder, bo.getRelatedOrder());
        lqw.eq(StringUtils.isNotBlank(bo.getBatchNo()), NetbarProductStockLog::getBatchNo, bo.getBatchNo());
        lqw.eq(bo.getSupplierId() != null, NetbarProductStockLog::getSupplierId, bo.getSupplierId());
        return lqw;
    }

    /**
     * 新增网吧商品库存流水
     *
     * @param bo 网吧商品库存流水
     * @return 是否新增成功
     */
    @Override
    public Boolean insertByBo(NetbarProductStockLogBo bo) {
        NetbarProductStockLog add = MapstructUtils.convert(bo, NetbarProductStockLog.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setLogId(add.getLogId());
        }
        return flag;
    }

    /**
     * 修改网吧商品库存流水
     *
     * @param bo 网吧商品库存流水
     * @return 是否修改成功
     */
    @Override
    public Boolean updateByBo(NetbarProductStockLogBo bo) {
        NetbarProductStockLog update = MapstructUtils.convert(bo, NetbarProductStockLog.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(NetbarProductStockLog entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 校验并批量删除网吧商品库存流水信息
     *
     * @param ids     待删除的主键集合
     * @param isValid 是否进行有效性校验
     * @return 是否删除成功
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteByIds(ids) > 0;
    }
}
